package com.kkb.controller;
import com.kkb.pojo.Team;
import com.kkb.vo.AjaxReslutVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-09-20 15:58
 * @Modified By:
 */
@Controller
public class RestfulController {
    private static List<Team> list;
    static {
        list = new ArrayList<>(4);
        for (int i=1; i<3;i++){
            Team team = new Team();
            team.setTeamId(1000+i);
            team.setTeamName("湖人"+i);
            team.setTeamDate(new Date());
            list.add(team);
        }
    }

    @RequestMapping(value = "/team/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxReslutVo<Team> delete(@PathVariable("id")int id){
        //遍历整个集合
        for ( Team t:list ) {
            if (t.getTeamId()==id){
                list.remove(t);
                return new AjaxReslutVo<Team>(200,"删除成功！");
            }
        }
        return new AjaxReslutVo<Team>(201,"删除失败！");
    }

    @RequestMapping(value = "/team/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public  AjaxReslutVo<Team> update(@PathVariable("id") int id,Team team){
        //遍历整个集合
        for ( Team t:list ) {
            if (t.getTeamId()==id){
                t.setTeamName(team.getTeamName());
                return new AjaxReslutVo<Team>(200,"更新成功！");
            }
        }
        return new AjaxReslutVo<Team>(201,"更新失败！");
    }

    @RequestMapping(value = "/team",method = RequestMethod.POST)
    @ResponseBody
    public AjaxReslutVo<Team> add(Team team){
        list.add(team);
        System.out.println(team);
        return new AjaxReslutVo<Team>(200,"添加成功！");
    }

    @RequestMapping(value = "/team/{id}",method = RequestMethod.GET)
    @ResponseBody
    public AjaxReslutVo<Team> getOne(@PathVariable(value = "id",required = false)int id){
        System.out.println("查询单个GET--发起的请求---------");
        for (Team team : list) {
            if(team.getTeamId()==id){
                return new AjaxReslutVo<Team>(200,"查询成功",team);
            }
        }
        return  null;
    }


    @RequestMapping(value = "/teams",method = RequestMethod.GET)
    @ResponseBody
    public AjaxReslutVo<Team> list(){
        System.out.println("进入teams方法中");
        return new AjaxReslutVo<Team>(200,"查询成功",list);
    }

    @RequestMapping("/hello")
    public String hello(){
        return "restful";
    }
}
